package com.ftg.learn.spring;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author kangjx
 * @date 2022/11/29
 */
public class IocDemo {


//    public static void main(String[] args) {
//        Kjx kjx = null;
//        String path = "/home/kangjx/workspace/java/back07/target/classes/com/ftg/learn/spring/demo.xml";
//        File xmlFile = new File(path);
//        SAXBuilder saxBuilder = new SAXBuilder();
//        try {
//            //文档  大量的  element组成
//            Document doc = saxBuilder.build(xmlFile);
//
//            System.out.println(doc.hasRootElement() +"判断是否有根");
//            Element root = doc.getRootElement();
//            System.out.println(root.getName() +"根节点的名字 ");
//
//            if(root.getName().equals("kjx")){
//                kjx = new Kjx();
//                //初始化一下我的子节点
//                kjx.setList(new ArrayList<>());
//
//                List<Element> childList = root.getChildren("superstart");
//                for (Element element : childList) {
//                    Element child = element;
//                    Attribute idAttr = child.getAttribute("id");
//                    Attribute classAttr = child.getAttribute("class");
//
//                    String idVal = idAttr.getValue();
//                    String classVal = classAttr.getValue();
//
//                    //子节点
//                    SuperStart temp = new SuperStart();
//                    temp.setId(idVal);
//                    temp.setClassName(classVal);
//
//                    kjx.getList().add(temp);
//                }
//
//            }else{
//                throw new RuntimeException("对不起..根必须叫kjx");
//            }
//            //Attribute 属性
//            Attribute rootAttr =  root.getAttribute("namespace");
//            System.out.println(rootAttr.getName()+" 属性名称");
//            System.out.println(rootAttr.getValue()+" 属性名称");
//
//
//        } catch (JDOMException | IOException e) {
//            e.printStackTrace();
//        }
//
//        System.out.println("输出对象");
//
//        kjx.getList().forEach(x->{
//            System.out.println(x.getClassName()+"您 注入的类的地址 ");
//            System.out.println(x.getId()+"您 声明id名称  ");
//        });
//
//    }

    public static void main(String[] args) {
        //mybatis : ---> xml 注入全是interface proxy 那么就是mybatis
        // -->xml 全是类，用反射那sprig ioc
        Kjx kjx = null;
        String path = "/home/kangjx/workspace/java/back07/target/classes/com/ftg/learn/spring/demo.xml";
        File xmlFile = new File(path);
        SAXBuilder saxBuilder = new SAXBuilder();
        try {
            //文档  大量的  element组成
            Document doc = saxBuilder.build(xmlFile);

            System.out.println(doc.hasRootElement() +"判断是否有根");
            Element root = doc.getRootElement();
            System.out.println(root.getName() +"根节点的名字 ");

            if(root.getName().equals("kjx")){
                kjx = new Kjx();
                //初始化一下我的子节点
                kjx.setSuperStartMap(new HashMap<>(1));

                List<Element> childList = root.getChildren("superstart");
                for (Element element : childList) {
                    Element child = element;
                    Attribute idAttr = child.getAttribute("id");
                    Attribute classAttr = child.getAttribute("class");

                    String idVal = idAttr.getValue();
                    String classVal = classAttr.getValue();

                    //子节点
                    SuperStart temp = new SuperStart();
                    temp.setId(idVal);
                    temp.setClassName(classVal);

                    if(kjx.getSuperStartMap().containsKey(idVal)){
                        throw new RuntimeException(idVal + "已经在superStartMap里注册了");
                    }

                    kjx.getSuperStartMap().put(idVal,temp);
                }



            }else{
                throw new RuntimeException("对不起..根必须叫kjx");
            }
            //Attribute 属性
            Attribute rootAttr =  root.getAttribute("namespace");
            System.out.println(rootAttr.getName()+" 属性名称");
            System.out.println(rootAttr.getValue()+" 属性名称");


        } catch (JDOMException | IOException e) {
            e.printStackTrace();
        }

        System.out.println("输出对象");

        kjx.getList().forEach(x->{
            System.out.println(x.getClassName()+"您 注入的类的地址 ");
            System.out.println(x.getId()+"您 声明id名称  ");
        });

    }


}
